<?php

namespace app\index\controller;

use app\admin\model\Goods;
use app\admin\model\GoodsCategory;
use app\admin\model\User;
use app\BaseController;
use think\facade\Db;

class Shop extends Base
{
    public function index()
    {
        // $data = Goods::with('category')->select(); 
        $cid = input('cid');
        $cate = Db::name('goodsCategory')->where(['status' => 1])->select();
        if (!empty($cid)) {
            $isHide = Db::name('goodsCategory')->where('cid', $cid)->value('status');
            if (!$isHide) return view('error/404', ['msg' => '分类不存在或已被删除']);
            $query = ['status' => 1, 'cid' => $cid];
            $cateName = Db::name('goodsCategory')->where('cid', $cid)->value('cname');
        } else {
            $query = ['status' => 1];
            $cateName = '全部';
        }
        
        $goods = Goods::list($query);
        $userData = (new User)->profile($this->user['user_id']);
        return view('index', ['cate' => $cate, 'goods' => $goods, 'userData' => $userData ,'cateName' => $cateName]);
    }
    // 积分付费
    public function pay()
    {
        if (request()->isPost()) {
            $goodId = input('id');
            $res = Goods::pay($goodId, $this->user['user_id']);
            return $res;
        }
    }
    // 积分充值
    public function score()   
    {
        $order = Db::name('tradeOrder')->where(['user_id' => $this->user['user_id']  , 'status' => 1])->select();
        return view('score' , ['orderList' => $order]);
    }
}
